package com.A21.GuoSai.demo.workflow_adjust.service;

import com.A21.GuoSai.demo.service_entity.entity.workflow.WorkFlowPojo;
import com.A21.GuoSai.demo.service_entity.utils.BpmnUtils;
import io.camunda.zeebe.client.ZeebeClient;
import io.camunda.zeebe.client.ZeebeClientBuilder;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ZeebeDeployService{
    //zeebe客户端构造器
    @Autowired
    private ZeebeClientBuilder zeebeClientBuilder;
    @Autowired
    private WorkFlowPojoService workFlowPojoService;

    /**
     * 根据流程bpmnProcessid和变量执行工作流
     * @param flowId
     * @param varibles
     */
    public void executeWorkFlow(String flowId,Object varibles){
        //运行实例这里没有使用容器中的client，
        //使用新建client的原因是防止单个client执行速度过慢
        ZeebeClient zeebeClient=zeebeClientBuilder.build();
        zeebeClient.newCreateInstanceCommand().bpmnProcessId(flowId)
//                .version(2)
                .latestVersion()
                .variables(varibles)
                .withResult()
                .send()
                .join();
    }


    /**
     * 保存工作流，保存了工作流自身的属性、条件结点以及变量的信息
     * @param workFlowPojo
     */
    @Transactional
    public void saveWorkerFlow(WorkFlowPojo workFlowPojo) throws Exception {
        workFlowPojoService.saveWorkerFlow(workFlowPojo);
    }
}




